package com.naiterui.ehp.bs.doctor.service.impl;

import com.naiterui.ehp.bp.domain.QuickReply;
import com.naiterui.ehp.bs.doctor.dao.IQuickReplyDao;
import com.naiterui.ehp.bs.doctor.repository.IQuickReplyRepository;
import com.naiterui.ehp.bs.doctor.service.IQuickReplyService;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class AdQuickReplyServiceImpl implements IQuickReplyService {

    @Autowired
    private IQuickReplyDao quickReplyDao;

    @Autowired
    private IQuickReplyRepository quickReplyRepository;

    @Override
    public List<QuickReply> findQuickReplyByDoctorId(Long doctorId, Integer type) {
        //默认查询会话类型
        type = type == null ? QuickReply.TYPE_CONSULT_SESSION : type;
        return quickReplyRepository.findByDoctorIdAndTypeOrderByIdDesc(doctorId, type);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveQuickReply(Long doctorId, String content, Integer type) {
        //默认会话类型
        type = type == null ? QuickReply.TYPE_CONSULT_SESSION : type;
        QuickReply quickReply = new QuickReply();
        quickReply.setDoctorId(doctorId);
        quickReply.setContent(content);
        quickReply.setType(type);
        quickReply.setCreatedAt(new Date());

        quickReplyDao.save(quickReply);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteQuickReplyByIds(String ids) {

        if (ids != null) {
            String[] idArr = ids.split(",");
            for (String id : idArr) {
                if (NumberUtils.isNumber(id)) {
                    quickReplyDao.removeById(Integer.valueOf(id));
                }
            }
        }

    }

}
